//
//  D1TabBarViewController.m
//  STBasicsApp
//
//  Created by jerry on 2019/5/3.
//  Copyright © 2019 jerry. All rights reserved.
//

#import "D1TabBarViewController.h"
#import "D1Item01UIViewController.h"
#import "D1Item02UIViewController.h"
#import "D1Item03UIViewController.h"
#import "D1Item04UIViewController.h"
#import "D1ItemMidUIViewController.h"

#import "STTabBar.h"

@interface D1TabBarViewController ()

@end

@implementation D1TabBarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // 初始化所有控制器
    [self setUpChildVC];
    
    STTabBar *tabBar = [[STTabBar alloc] init];
    [self setValue:tabBar forKey:@"tabBar"];
    
    [tabBar setDidClickPublishBtn:^{
//        D1ItemMidUIViewController *hmpositionVC = [[D1ItemMidUIViewController alloc] init];
//        STBaseNavigationViewController * nav = [[STBaseNavigationViewController alloc] initWithRootViewController:hmpositionVC];
//        [self presentViewController:nav animated:YES completion:nil];
    }];
}

- (void)setUpChildVC {
    
    D1Item01UIViewController *item01VC = [[D1Item01UIViewController alloc] init];
    [self setChildVC:item01VC title:@"首页" image:@"st_icon_tabbar_item01_n" selectedImage:@"st_icon_tabbar_item01_s"];
    
    D1Item02UIViewController *item02VC = [[D1Item02UIViewController alloc] init];
    [self setChildVC:item02VC title:@"发现" image:@"st_icon_tabbar_item02_n" selectedImage:@"st_icon_tabbar_item02_s"];
    
    D1Item03UIViewController *item03VC = [[D1Item03UIViewController alloc] init];
    [self setChildVC:item03VC title:@"卡券" image:@"st_icon_tabbar_item03_n" selectedImage:@"st_icon_tabbar_item03_s"];
    
    D1Item04UIViewController *item04VC = [[D1Item04UIViewController alloc] init];
    [self setChildVC:item04VC title:@"我的" image:@"st_icon_tabbar_item04_n" selectedImage:@"st_icon_tabbar_item04_s"];
    
}

- (void) setChildVC:(UIViewController *)childVC title:(NSString *) title image:(NSString *) image selectedImage:(NSString *) selectedImage {
    
    childVC.tabBarItem.title = title;
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    dict[NSForegroundColorAttributeName] = [UIColor blackColor];
    dict[NSFontAttributeName] = [UIFont systemFontOfSize:10];
    [childVC.tabBarItem setTitleTextAttributes:dict forState:UIControlStateNormal];
    childVC.tabBarItem.image = [[UIImage imageNamed:image] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    childVC.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImage] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    STBaseNavigationViewController *nav = [[STBaseNavigationViewController alloc] initWithRootViewController:childVC];
    [self addChildViewController:nav];
}

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{
    
    NSLog(@"item name = %@", item.title);
    
    NSInteger index = [self.tabBar.items indexOfObject:item];
    [self animationWithIndex:index];
    
    
    if([item.title isEqualToString:@"发现"])
    {
        // 也可以判断标题,然后做自己想做的事
    }
}
- (void)animationWithIndex:(NSInteger) index {
    
    NSMutableArray * tabbarbuttonArray = [NSMutableArray array];
    
    for (UIView *tabBarButton in self.tabBar.subviews) {
        
        if ([tabBarButton isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            [tabbarbuttonArray addObject:tabBarButton];
        }
    }
    CABasicAnimation*pulse = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    pulse.timingFunction= [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    pulse.duration = 0.2;
    pulse.repeatCount= 1;
    pulse.autoreverses= YES;
    pulse.fromValue= [NSNumber numberWithFloat:0.7];
    pulse.toValue= [NSNumber numberWithFloat:1.3];
    [[tabbarbuttonArray[index] layer] addAnimation:pulse forKey:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
